package com.qpf.mycms.website.member.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qpf.mycms.api.exception.SystemException;
import com.qpf.mycms.api.exception.enums.ClientExceptionEnum;
import com.qpf.mycms.api.factory.PageFactory;
import com.qpf.mycms.api.factory.PageResultFactory;
import com.qpf.mycms.api.pojo.MemberOpenidRequest;
import com.qpf.mycms.api.pojo.PageResult;
import com.qpf.mycms.api.pojo.UserRequest;
import com.qpf.mycms.website.member.domain.MemberOpenid;
import com.qpf.mycms.website.member.service.MemberOpenidService;
import com.qpf.mycms.website.member.mapper.MemberOpenidMapper;
import com.qpf.mycms.website.member.service.MemberUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
* @author shadaileng
* @description 针对表【member_openid(第三方会员)】的数据库操作Service实现
* &#064;createDate  2024-08-10 21:12:40
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MemberOpenidServiceImpl extends ServiceImpl<MemberOpenidMapper, MemberOpenid>
    implements MemberOpenidService{

    @Resource
    private MemberUserService memberUserService;
    @Override
    public void add(MemberOpenidRequest request) {
        MemberOpenid memberOpenid = new MemberOpenid();
        long nextId = IdUtil.getSnowflakeNextId();
        BeanUtils.copyProperties(request, memberOpenid);
        memberOpenid.setUserId(nextId);
        save(memberOpenid);
        UserRequest userRequest = new UserRequest();
        BeanUtils.copyProperties(request, userRequest);
        userRequest.setUsername(request.getNickname());
        userRequest.setId(nextId);
        memberUserService.add(userRequest);
    }

    @Override
    public void del(MemberOpenidRequest request) {
        MemberOpenid memberOpenid = findOne(request);
        removeById(memberOpenid.getId());
    }

    @Override
    public void edit(MemberOpenidRequest request) {
        findOne(request);
        MemberOpenid memberOpenid = new MemberOpenid();
        BeanUtils.copyProperties(request, memberOpenid);
        updateById(memberOpenid);
    }

    @Override
    public MemberOpenid detail(MemberOpenidRequest request) {
        return findOne(request);
    }

    private MemberOpenid findOne(MemberOpenidRequest request) {
        if (ObjectUtils.isEmpty(request)) {
            throw new SystemException(ClientExceptionEnum.PARAM_NOT_NULL, "参数");
        }
        MemberOpenid memberOpenid;
        if (ObjectUtils.isEmpty(request.getId())) {
            memberOpenid = getOne(createWrapper(request));
        } else {
            memberOpenid = getById(request.getId());
        }
        if (ObjectUtils.isEmpty(memberOpenid)) {
            throw new SystemException(ClientExceptionEnum.USER_NOT_EXITS);
        }
        return  memberOpenid;
    }

    @Override
    public PageResult<MemberOpenid> findPage(MemberOpenidRequest request) {
        Page<MemberOpenid> page = page(PageFactory.defaultPage(), createWrapper(request));
        return PageResultFactory.createPageResult(page);
    }

    private LambdaQueryWrapper<MemberOpenid> createWrapper(MemberOpenidRequest request) {
        LambdaQueryWrapper<MemberOpenid> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isEmpty(request)) {
            return wrapper;
        }
        wrapper.eq(!ObjectUtils.isEmpty(request.getId()), MemberOpenid::getId, request.getId());
        wrapper.eq(!ObjectUtils.isEmpty(request.getUserId()), MemberOpenid::getUserId, request.getUserId());
        wrapper.eq(!ObjectUtils.isEmpty(request.getValue()), MemberOpenid::getValue, request.getValue());
        return wrapper;
    }


    public int queryCount(MemberOpenidRequest request) {
        return this.count(createWrapper(request));
    }
}




